package com.chenglai.pay.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.pay.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 类描述：支付
 *
 * @Author rxh
 * @Date 2023/11/21 10:24
 */
@RestController()
@RequestMapping(value = "/pay")
@Api(value = "/pay", tags = {"支付"})
public class PayController {

    @Resource
    private PayService payService;

    /**
     * 支付需要场景值：scene: H5 JSAPI APP
     * 如传H5 需要传场景信息 scene_info：{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}
     * 如传JSAPI 需要传用户标识 openid: oUpF8uMuAJO_M2pxb1Q9zNjWeS6o
     * 如传APP 和之前一样
     *
     * @param shopId   门店id
     * @param orderNum 订单号
     * @param totalNum 金额
     * @param shopName 门店名称
     * @param scene    场景值
     * @param request
     * @return
     */
    @ApiOperation(value = "app支付")
    @PostMapping("/pay")
    public Result pay(@ApiParam(name = "shopId", value = "门店id") Integer shopId,
                      @ApiParam(name = "orderNum", value = "订单号") String orderNum,
                      @ApiParam(name = "totalNum", value = "金额") Double totalNum,
                      @ApiParam(name = "shopName", value = "门店名称") String shopName,
                      @ApiParam(name = "scene", value = "场景值：H5 JSAPI APP") String scene,
                      @ApiParam(name = "sceneInfo", value = "场景信息：scene=H5需传") String sceneInfo,
                      @ApiParam(name = "openId", value = "openId scene=JSAPI需传") String openId,
                      @ApiParam(name = "userId", value = "用户ID") Integer userId,
                      HttpServletRequest request) {
        try {
            Map map = payService.getPackages(shopId, orderNum, totalNum, shopName, scene, sceneInfo, openId,userId, request);
            return new Result(true, StatusCode.OK, "操作成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    @ApiOperation(value = "支付回调")
    @PostMapping("/paySuccess")
    public Result paySuccess(HttpServletRequest request, HttpServletResponse response) {

        try {
            payService.getSuccess(request, response);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }


    /**
     * 退款
     *
     * @param transactionId 微信支付流水号
     * @param shopId        门店id
     * @param totalNum      订单金额
     * @param refundFee     退款金额
     * @param refundDesc    退款备注
     * @return
     */
    @ApiOperation(value = "退款")
    @PostMapping("/refund")
    public Result refund(String transactionId, Integer shopId, Double totalNum, Double refundFee, String refundDesc) {
        try {
            Map map = payService.refund(transactionId, shopId, totalNum, refundFee, refundDesc);
            return new Result(true, StatusCode.OK, "操作成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "操作失败");
        }
    }


    @ApiOperation(value = "退款回调")
    @PostMapping("/refundSuccess")
    public Result refundSuccess(HttpServletRequest request, HttpServletResponse response) {
        try {
            payService.refundSuccess(request, response);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }


}
